/**
 * 
 * @authors cherish yii2 (cherish@cherish.pw)
 * @date    2020-12-10 16:48:28
 * @version v1.0
 * @description the core js of todolist project
 * 
 * ━━━━━━神兽出没━━━━━━
 * 　　   ┏┓　 ┏┓
 * 　┏━━━━┛┻━━━┛┻━━━┓
 * 　┃              ┃
 * 　┃       ━　    ┃
 * 　┃　  ┳┛ 　┗┳   ┃
 * 　┃              ┃
 * 　┃       ┻　    ┃
 * 　┃              ┃
 * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
 *       ┃      ┃     神兽保佑,代码无bug。
 *       ┃      ┃
 *       ┃      ┗━━━┓
 *       ┃      　　┣┓
 *       ┃      　　┏┛
 *       ┗━┓┓┏━━┳┓┏━┛
 *     　  ┃┫┫　┃┫┫
 *     　  ┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

// 请根据考试说明文档中列出的需求进行作答
// 预祝各位顺利通过本次考试，see you next week！
// ...
$(function(){
    var arr={
        wanc:['吃饭','睡觉','打豆豆'],
        weiwanc:['喝酒','蹦迪']
    };
    if(localStorage.getItem('obj')==null){
        localStorage.setItem('obj',JSON.stringify(arr))
    }
    var lshi =JSON.parse(localStorage.getItem('obj'));
    wanc_sc='';
    weiwanc_sc='';
    for(var i=0;i<lshi.wanc.length;i++){
        wanc_sc+=`<li>
                    <input type="checkbox" />
                    <p>${lshi.wanc[i]}</p>
                    <a href="">-</a>
                </li>`
    }
    for(var i=0;i<lshi.weiwanc.length;i++){
        weiwanc_sc+=`<li>
                    <input type="checkbox"  checked="checked"/>
                    <p>${lshi.weiwanc[i]}</p>
                    <a href="">-</a>
                </li>`
    }
    $('#todolist').append(wanc_sc);
    $('#donelist').append(weiwanc_sc);
    //计数
    function jishu(){
        var shul=$('#todolist').children().length
        $('#todocount').text(shul);
        var shul=$('#donelist').children().length
        $('#donecount').text(shul);
    }
    //add添加
    function add(event){
        if(event.keyCode==13){
            event.preventDefault();
            var shurushuu=$('#title').val().trim();
            s=`
                <li>
                    <input type="checkbox" />
                    <p>${shurushuu}</p>
                    <a href="">-</a>
                </li>`
            if(shurushuu!==''){
                $('#todolist').append(s);
                $('#title').val('');
                lshi =JSON.parse(localStorage.getItem('obj'));
                lshi.wanc.push(shurushuu)
                localStorage.setItem('obj',JSON.stringify(lshi));
                jishu();
            }
        }
    }
    //remove删除
    function remove(event){
        if(event.target.localName=='a'){
            event.preventDefault();
            event.target.parentNode.remove();
            jishu();
            if(!event.target.parentNode.childNodes[1].checked){
                lshi =JSON.parse(localStorage.getItem('obj'));
                var lshs=event.target.parentNode.childNodes[3].innerText;
                lshi.wanc.splice(lshi.wanc.indexOf(lshs),1)
                localStorage.setItem('obj',JSON.stringify(lshi));
            }else{
                lshi =JSON.parse(localStorage.getItem('obj'));
                var lshs=event.target.parentNode.childNodes[3].innerText;
                lshi.weiwanc.splice(lshi.weiwanc.indexOf(lshs),1)
                localStorage.setItem('obj',JSON.stringify(lshi));
            }
            
        }
    }
    //update编辑文字
    function update(event){
        if(event.target.localName=='p'){
            event.target.contentEditable='true';
            event.target.designMode='on';
            console.dir(event.target);
            // $('body').change(function(){
            //     console.log(2);
            // })
        }
    }
    //勾选切换状态
    function qiehuan(event){
        if(event.target.localName=='input'){
            if(event.target.checked){
                var xuanzhong=event.target.parentNode;
                $('#donelist').append(xuanzhong);
                jishu();
                lshi =JSON.parse(localStorage.getItem('obj'));
                var lshs=event.target.parentNode.childNodes[3].innerText;
                lshi.wanc.splice(lshi.wanc.indexOf(lshs),1)
                lshi.weiwanc.push(lshs)
                localStorage.setItem('obj',JSON.stringify(lshi));
            }else{
                var meizhong=event.target.parentNode;
                $('#todolist').append(meizhong);
                jishu();
                lshi =JSON.parse(localStorage.getItem('obj'));
                var lshs=event.target.parentNode.childNodes[3].innerText;
                lshi.weiwanc.splice(lshi.weiwanc.indexOf(lshs),1)
                lshi.wanc.push(lshs)
                localStorage.setItem('obj',JSON.stringify(lshi));
            }
        }
    }
    $('#title').keydown(add);//添加
    $('#jiangtingshi').on('click',remove).//删除
    on('click',update).//编辑文字
    on('click',qiehuan)//切换状态
})